package 练习题;

import java.util.Arrays;

import static java.util.Arrays.sort;

/**
 * @author Liangyaoyun
 */
public class IntArrayList {

    private Integer[] elementData;
    private Integer size = 0;

    /**
     * 无参构造器
     */
    public IntArrayList() {
        elementData = new Integer[10];
    }

    /**
     * 有参构造器
     *
     * @param capacity
     */
    public IntArrayList(int capacity) {

        if (capacity < 0) {
            throw new IllegalArgumentException("参数越界：" + capacity);
        }

        elementData = new Integer[capacity];
    }

    /**
     * 扩容
     */
    public void extendLength(int minCapacity) {
        int oldLength = elementData.length;
        int newLength = oldLength + (oldLength >> 1);
        if (newLength - minCapacity < 0) {
            newLength = minCapacity;
        }
        Integer[] tmp = new Integer[newLength];
        for (int i = 0; i < elementData.length; i++) {
            tmp[i] = elementData[i];
        }
        elementData = tmp;
    }

    /**
     * 元素追加方法
     *
     * @param e
     */
    public void add(int e) {

        int minCapacity = (size + 1);

        //扩容
        if (minCapacity - elementData.length > 0) {
            extendLength(minCapacity);
        }

        elementData[size] = e;
        size++;
    }

    /**
     * 向指定下标index插入新元素e的方法
     *
     * @param index
     * @param e
     */
    public void insert(int index, int e) {

        if (index < 0 || index > size) {
            throw new IllegalArgumentException("参数越界：" + index);
        }

        int minCapacity = (size + 1);

        //扩容
        if (minCapacity - elementData.length > 0) {
            extendLength(minCapacity);
        }

        for (int i = size - 1; i >= index; i--) {
            elementData[i + 1] = elementData[i];
        }
        elementData[index] = e;
        size++;
    }

    /**
     * 按照下标删除指定位置元素的方法
     *
     * @param index
     * @return
     */
    public Integer remove(int index) {

        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("参数越界：" + index);
        }

        int delete = elementData[index];

        for (int i = index; i < size; i++) {
            elementData[i] = elementData[i + 1];
        }
        size--;

        return delete;
    }

    /**
     * 获取指定下标元素的方法
     *
     * @param index
     * @return
     */
    public Integer get(int index) {

        if (index >= size || index < 0) {
            throw new IllegalArgumentException("参数越界：" + index);
        } else {
            return elementData[index];
        }
    }

    /**
     * 获取全部元素
     */
    public void printf() {
        int[] array = new int[size];
        for (int i = 0; i < size; i++) {
            array[i] = elementData[i];
        }
        System.out.println(Arrays.toString(array));
    }

    /**
     * 将内部数组长度修改为与数组中有效元素数量相同长度的方法
     */
    public void trimToSize() {
        Integer[] tmp = new Integer[size];
        for (int i = 0; i < size; i++) {
            tmp[i] = elementData[i];
        }
        elementData = tmp;
    }

    /**
     * 返回当前数组长度
     */
    public int Long() {
        return elementData.length;
    }
}
